<template>
  <n-config-provider :theme="naiveTheme" :theme-overrides="themeOverrides">
    <n-global-style />
    <n-message-provider>
      <n-dialog-provider>
        <n-notification-provider>
          <n-loading-bar-provider>
            <router-view v-slot="{ Component }">
              <transition
                name="page"
                mode="out-in"
                appear
              >
                <component :is="Component" :key="route.path" />
              </transition>
            </router-view>
          </n-loading-bar-provider>
        </n-notification-provider>
      </n-dialog-provider>
    </n-message-provider>
  </n-config-provider>
</template>

<script setup lang="ts">
import { computed, watch, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { darkTheme } from 'naive-ui'
import { useThemeStore } from '@/store'

const route = useRoute()
const themeStore = useThemeStore()

// Naive UI 主题配置
const naiveTheme = computed(() => {
  const theme = themeStore.isDark ? darkTheme : null
  console.log('🎨 Naive UI theme changed:', theme ? 'dark' : 'light', 'isDark:', themeStore.isDark)
  return theme
})

// 主题覆盖配置
const themeOverrides = computed(() => {
  console.log('🔧 Theme overrides computing, isDark:', themeStore.isDark)
  
  if (themeStore.isDark) {
    return {
      common: {
        primaryColor: '#409eff',
        primaryColorHover: '#66b1ff',
        primaryColorPressed: '#3a8ee6',
        primaryColorSuppl: '#409eff',
        
        // 背景色
        bodyColor: '#0f172a',
        popoverColor: '#1e293b',
        cardColor: '#0f172a',
        modalColor: '#1e293b',
        
        // 文字颜色
        textColorBase: '#ffffff',
        textColor1: '#ffffff',
        textColor2: '#e2e8f0',
        textColor3: '#cbd5e1',
        
        // 边框颜色
        borderColor: '#334155',
        dividerColor: '#334155',
        
        // 输入框
        inputColor: '#1e293b',
        inputColorFocus: '#334155',
        
        // 表格
        tableColor: '#0f172a',
        tableColorHover: '#1e293b',
        tableHeaderColor: '#1e293b',
        
        // 按钮
        buttonColor2: '#1e293b',
        buttonColor2Hover: '#334155',
        buttonColor2Pressed: '#475569',
      }
    }
  } else {
    return {
      common: {
        primaryColor: '#409eff',
        primaryColorHover: '#66b1ff',
        primaryColorPressed: '#3a8ee6',
        primaryColorSuppl: '#409eff',
      }
    }
  }
})

// 监听主题变化
watch(() => themeStore.isDark, (newValue, oldValue) => {
  console.log('🌙 Theme changed from', oldValue, 'to', newValue)
  console.log('📱 HTML class list:', document.documentElement.classList.toString())
}, { immediate: true })

// 组件挂载时的调试信息
onMounted(() => {
  console.log('🚀 App.vue mounted')
  console.log('🎯 Initial theme state:', themeStore.isDark)
  console.log('🏷️ HTML classes:', document.documentElement.classList.toString())
  
  // 手动触发一次主题检查
  themeStore.initTheme()
})
</script>

<style scoped>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--text-color-primary);
  background-color: var(--bg-color-primary);
  transition: background-color 0.3s ease, color 0.3s ease;
}

/* 简化的页面切换动画 */
.page-enter-active,
.page-leave-active {
  transition: all 0.3s ease;
}

.page-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.page-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}

.page-enter-to,
.page-leave-from {
  opacity: 1;
  transform: translateX(0);
}
</style> 